﻿namespace demo
{
    using System.Windows.Controls;
    using System.Windows.Navigation;

    using demo.ViewModels;
    using System.Windows;
    using demo.Views;

    /// <summary>
    /// Home page for the application.
    /// </summary>
    public partial class Home : Page
    {
        private HomeVM vm = new HomeVM();


        /// <summary>
        /// Creates a new <see cref="Home"/> instance.
        /// </summary>
        public Home()
        {
            InitializeComponent();

            this.Title = ApplicationStrings.HomePageTitle;
            this.DataContext = vm;

        }

        /// <summary>
        /// Executes when the user navigates to this page.
        /// </summary>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            vm.LoadDepts();
        }


        private void dgDepts_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            vm.LoadEmployeesForDept();

        }

        private void btnSaveChanges_Click(object sender, RoutedEventArgs e)
        {
            vm.SaveChanges();
        }

        private void btnNewDept_Click(object sender, RoutedEventArgs e)
        {
            AddDept cw = new AddDept();

            cw.Closed += (o, ea) =>
            {
                if (cw.DialogResult == true)        // user clicked 'ok'
                {
                    vm.AddDept(cw.tbName.Text);
                }
                else { }                            // user clicked cancel or closed the dialog
                
            };

            cw.Show();

        }

        private void btnNewEmp_Click(object sender, RoutedEventArgs e)
        {
            AddEmp cw = new AddEmp();
            cw.Closed += (o, ea) =>
            {
                if (cw.DialogResult == true)        // user clicked 'ok'
                {
                    vm.AddEmp(vm.SelectedDepartment.ID, cw.tbName.Text, cw.tbAddr.Text, cw.tbExt.Text);
                }
                else
                {                              // user clicked cancel or closed the dialog
                }
            };

            cw.Show();
        }


    }
}